<template>
  <div id="userLoginPage">
    <h2 class="title">良辰云图库 - 用户登录</h2>
    <div class="desc">良辰AI云图库</div>
    <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码长度不能小于 8 位' },
        ]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
      </a-form-item>


      <div class="captcha-container">
        <a-form-item name="captcha" :rules="[{ required: true, message: '请输入验证码' }]">
          <a-input v-model:value="formState.captcha" placeholder="请输入验证码" class="captcha-input"/>
        </a-form-item>
        <img :src="captchaImageUrl" alt="验证码" @click="refreshCaptcha" class="captcha-image"/>
      </div>



      <div class="tips">
        没有账号？
        <RouterLink to="/user/register">去注册</RouterLink>
      </div>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive,ref } from 'vue'
import { userLoginUsingPost,getCaptchaUsingGet } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { message } from 'ant-design-vue'
import router from '@/router' // 用于接受表单输入的值
// 用于接受表单输入的值
const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
  captcha: '', // 验证码
})

// 验证码图片URL
const captchaImageUrl = ref<string>('')





const refreshCaptcha = async () => {
  try {
    // 假设 getCaptchaUsingGet 是一个封装了 Axios GET 请求的函数
    const response = await getCaptchaUsingGet();
    // 获取图片blob
    const blob = new Blob([response.data], { type: response.headers['content-type'] });
    // 创建一个对象URL
    const imageUrl = URL.createObjectURL(blob);
    captchaImageUrl.value = imageUrl;

    console.log("验证码图片URL：" + captchaImageUrl.value);
  } catch (error) {
    console.error('Fetching captcha failed:', error);
    // 处理错误情况，例如显示错误消息或重试
  }
};



const loginUserStore = useLoginUserStore()
/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  const res = await userLoginUsingPost(values)
  // 登录成功，把登录态保存到全局状态中
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')
    router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error('登录失败，' + res.data.message)
  }
}



// 页面加载时刷新验证码
refreshCaptcha()
</script>

<style scoped>
#userLoginPage {
  max-width: 360px;
  margin: 0 auto;
}
.title {
  text-align: center;
  margin-bottom: 16px;
}
.desc {
  text-align: center;
  color: #bbb;
  margin-bottom: 16px;
}
.tips {
  color: #bbb;
  text-align: right;
  font-size: 13px;
  margin-bottom: 16px;
}

/* 验证码图片样式 */

.captcha-container {
  display: flex;
  align-items: center;
  gap: 40px; /* 设置输入框和图片之间的间隙 */
}

.captcha-input {
  width: 230px;
  margin-left: 10px; /* 调整图片与输入框之间的间距 */
  cursor: pointer; /* 添加手型光标，表示可点击 */
}

.captcha-image {
  height: 40px;
  margin-left: 10px; /* 调整图片与输入框之间的间距 */
  cursor: pointer; /* 添加手型光标，表示可点击 */
}

/* 背景图片 */
.ant-layout-content.content {
  background-image: url('/backgroundPicture.jpg') !important; /* 替换为您的图片URL */
  background-position: center; /* 图片居中 */
  background-repeat: no-repeat; /* 不重复图片 */
  background-attachment: fixed; /* 固定背景图片 */
  background-size: cover; /* 覆盖整个页面 */
}



</style>
